package com.zr.action;

import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;

import javax.annotation.Resource;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zr.model.Car;
import com.zr.model.Orders;
import com.zr.service.AddCarService;
import com.zr.service.CarSreivce;

@Controller
@Scope(value="prototype")
public class AddCarAction implements ServletResponseAware{
private String plate;
private String volume;
private String weight;
private String purchasing;
private String state;
private String driver;
private String position;
private int cid;
private int cids[];
private int oid;
private String Orderstatus;

public int getOid() {
	return oid;
}
public void setOid(int oid) {
	this.oid = oid;
}
public String getOrderstatus() {
	return Orderstatus;
}
public void setOrderstatus(String orderstatus) {
	Orderstatus = orderstatus;
}
public String getPosition() {
	return position;
}
public void setPosition(String position) {
	this.position = position;
}
public int[] getCids() {
	return cids;
}
public void setCids(int[] cids) {
	this.cids = cids;
}
public int getCid() {
	return cid;
}
public void setCid(int cid) {
	this.cid = cid;
}
public String getPlate() {
	return plate;
}
public void setPlate(String plate) {
	this.plate = plate;
}
public String getVolume() {
	return volume;
}
public void setVolume(String volume) {
	this.volume = volume;
}
public String getWeight() {
	return weight;
}
public void setWeight(String weight) {
	this.weight = weight;
}
public String getPurchasing() {
	return purchasing;
}
public void setPurchasing(String purchasing) {
	this.purchasing = purchasing;
}
public String getState() {
	return state;
}
public void setState(String state) {
	this.state = state;
}
public String getDriver() {
	return driver;
}
public void setDriver(String driver) {
	this.driver = driver;
}
@Resource
private  AddCarService  addcarService;
private ServletResponse req;
public void addCar(){	
Car i=	addcarService.addCar(plate, volume, weight, purchasing, state, driver,position);
try {
	if(i!=null){
	Writer pw=req.getWriter();
	pw.write("1");}
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
}
public void updateCar(){
	Car i=addcarService.updateCar(plate, volume, weight, purchasing, state, driver,position, cid);
	try {
		if(i!=null){
			Writer pw=req.getWriter();
			pw.write("1");
		}
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
public void deleteCar(){	
	for(int i=0;i<cids.length;i++){ 
		int cid=cids[i];
		addcarService.deleteCar(cid);	 
    } 
	Writer pw;
	try {
		pw = req.getWriter();
		pw.write("1");
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}
public void updateOrderstatus(){	
	Orders i=addcarService.updateOrderstatus(plate, Orderstatus, oid);	
	try {
		if(i!=null){
			Writer pw=req.getWriter();
			pw.write("1");
		}
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
public void updateCarPosition(){
	Car i=addcarService.updateCarPosition(position,state, cid);
	if(i!=null){
		try {
			Writer pw=req.getWriter();
			pw.write("1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
public void cxplate(){
	int i=addcarService.findPlates(plate);
	if(i>0){
		try {
			Writer pw=req.getWriter();
			pw.write("1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
@Override
public void setServletResponse(HttpServletResponse Response) {
	this.req=Response;
	
}

}
